RETRIEVE email LIST- java

To retrieve a detailed log of your messaging history, use the following code:

import com.google.gson.FieldNamingPolicy;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

import com.google.gson.JsonArray;

import com.google.gson.JsonObject;

import com.google.gson.JsonPrimitive;

 

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

 

public class Default {

        public static String apiKey = "ApiKey";

 

        public static void main(String[] args) throws Exception {

               try {

                       getEmailList();

               }

               catch(Exception ex) {

                       System.out.println(ex.getMessage());

               }

        }

 

        private static void getEmailList() throws Exception {

 

               String url = "https://messagingapis.paylite.net/api/messagingdata/getemaillist";

 

               Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();

 

               JsonObject jsonData = new JsonObject();

               jsonData.addProperty("ApiKey", apiKey);

               jsonData.addProperty("StartDate", "1/1/2018");

               jsonData.addProperty("EndDate", "1/23/2018");

 

               String postJsonData = gson.toJson(jsonData);

               System.out.println(postJsonData);

 

               postRequest(url, postJsonData);

        } 

         

 

        private static void postRequest(String url, String postJsonData) throws Exception {

               URL obj = new URL(url);

               HttpURLConnection con = (HttpURLConnection) obj.openConnection();

 

               // Setting basic post request

               con.setRequestMethod("POST");

               con.setRequestProperty("Content-Type", "application/json");

 

               // Send post request

               con.setDoOutput(true);

               DataOutputStream wr = new DataOutputStream(con.getOutputStream());

               wr.writeBytes(postJsonData);

               wr.flush();

               wr.close();

 

               //Read output

               BufferedReader in =new BufferedReader(new InputStreamReader(con.getInputStream()));

               String output;

               StringBuffer response = new StringBuffer();

 

               while ((output = in.readLine()) != null) {

                       response.append(output);

               } in .close();

 

               // printing result from response

               System.out.println(response.toString());

        }

}